package  com.ychug.service.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ychug.common.constant.UserConstants;
import com.ychug.service.cms.entity.CmsCategoryEntity;
import com.ychug.service.cms.service.ICmsCategoryService;
import com.ychug.service.system.entity.SysDeptEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ychug.service.cms.mapper.CmsCategoryMapper;

import com.ychug.common.support.Convert;

/**
 * 栏目 服务层实现
 * 
 * @author mcpcms
 * @date 2019-04-27
 */
@Service
public class CmsCategoryServiceImpl implements ICmsCategoryService
{
	@Autowired
	private CmsCategoryMapper cmsCategoryMapper;

	/**
     * 查询栏目信息
     * 
     * @param categoryId 栏目ID
     * @return 栏目信息
     */
    @Override
	public CmsCategoryEntity selectCmsCategoryById(Integer categoryId)
	{
	    return cmsCategoryMapper.selectCmsCategoryById(categoryId);
	}
	
	/**
     * 查询栏目列表
     * 
     * @param cmsCategory 栏目信息
     * @return 栏目集合
     */
	@Override
	public List<CmsCategoryEntity> selectCmsCategoryList(CmsCategoryEntity cmsCategory)
	{
	    return cmsCategoryMapper.selectCmsCategoryList(cmsCategory);
	}
	
    /**
     * 新增栏目
     * 
     * @param cmsCategory 栏目信息
     * @return 结果
     */
	@Override
	public int insertCmsCategory(CmsCategoryEntity cmsCategory)
	{
	    return cmsCategoryMapper.insertCmsCategory(cmsCategory);
	}
	
	/**
     * 修改栏目
     * 
     * @param cmsCategory 栏目信息
     * @return 结果
     */
	@Override
	public int updateCmsCategory(CmsCategoryEntity cmsCategory)
	{
	    return cmsCategoryMapper.updateCmsCategory(cmsCategory);
	}

	/**
     * 删除栏目对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteCmsCategoryByIds(String ids)
	{
		return cmsCategoryMapper.deleteCmsCategoryByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<Map<String, Object>> selectCmsCategoryTree() {
		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		List<CmsCategoryEntity> cmsCateList = selectCmsCategoryList(new CmsCategoryEntity());
		trees = getTrees(cmsCateList, false, null);
		return trees;
	}


	/**
	 * 对象转部门树
	 *
	 * @param cateList 栏目列表
	 * @param isCheck 是否需要选中
	 * @param roleDeptList 角色已存在菜单列表
	 * @return
	 */
	public List<Map<String, Object>> getTrees(List<CmsCategoryEntity> cateList, boolean isCheck, List<String> roleDeptList)
	{

		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		for (CmsCategoryEntity cate : cateList)
		{
			if (UserConstants.CATE_NORMAL.equals(String.valueOf(cate.getStatus())))
			{
				Map<String, Object> deptMap = new HashMap<String, Object>();
				deptMap.put("id", cate.getCatId());
				deptMap.put("pId", cate.getParentId());
				deptMap.put("name", cate.getCatName());
				deptMap.put("title", cate.getTitle());
				if (isCheck)
				{
					deptMap.put("checked", roleDeptList.contains(cate.getCatId() + cate.getCatName()));
				}
				else
				{
					deptMap.put("checked", false);
				}
				trees.add(deptMap);
			}
		}
		return trees;
	}


}
